<?

if (extension_loaded('gettext')) {
	if (isset($_COOKIE['lang'])) {
		setlocale(LC_MESSAGES,  $_COOKIE['lang']);
//    setcookie("lang",  $_COOKIE['lang'], time()+2592000);
  } else {
			setlocale(LC_MESSAGES,  'en_US');
	}
	bindtextdomain('main','../../i18n');
  textdomain('main');
}


function uploader($file_types_array=array("gpg"), $max_file_size=100000000, $upload_dir="/tmp/"){
  if(!is_numeric($max_file_size)){
   $max_file_size = 100000000;
  }
  if(!isset($_POST["submitted"])){
   echo "<form name='upload' action='".$PHP_SELF."' method='post' enctype='multipart/form-data'>";
   echo _("Choose File:");
   echo "&nbsp;";
   echo "<input type='hidden' name='submitted' value='TRUE' id='".time()."'><input type='hidden' name='MAX_FILE_SIZE' value='".$max_file_size."'>";
   echo "<input type='file' name='file[]'>";
   echo "&nbsp;<input type='submit' value='";
   echo _("Send");
   echo "'>\n";
   echo "</form>";
   echo "\n<b><font color='#FF0000'>";
   echo _("Warning:");
   echo "</font></b>\n\n";
   echo _("When the updater running please Don't use Phones connected to");
   echo " <b>Voiper PBX</b>, ";
   echo _(" \nwhen the update is done Voiper PBX will be restarted.");
   echo "\n\n";
   echo _("Files for updates must be downloaded from the site");
   echo " <a href='http://www.voiper.it' target='_blank'>Voiper.it</a><br>";
   echo _("with the extension");
   echo "<b>.GPG</b>";
  }else{
   foreach($_FILES["file"]["error"] as $key => $value){
     if($_FILES["file"]["name"][$key]!=""){
       if($value==UPLOAD_ERR_OK){
         $origfilename = $_FILES["file"]["name"][$key];
         $filename = explode(".", $_FILES["file"]["name"][$key]);
         $filenameext = $filename[count($filename)-1];
         unset($filename[count($filename)-1]);
         $filename = implode(".", $filename);
         $filename = substr($filename, 0, 40).".".$filenameext;
         $file_ext_allow = FALSE;
         for($x=0;$x<count($file_types_array);$x++){
           if($filenameext==$file_types_array[$x]){
             $file_ext_allow = TRUE;
           }
         }
         if($file_ext_allow){
           if($_FILES["file"]["size"][$key]<$max_file_size){
             if(move_uploaded_file($_FILES["file"]["tmp_name"][$key], $upload_dir.$filename)){
 
             $rimuovi_file_0="/bin/rm -rf /tmp/update_dec.tar";
		         exec("$rimuovi_file_0 2>&1", $out0, $removeerror);
					     
						 foreach($out0 as $outputline){
						    echo("$outputline<br>");
						 }

        $decripta="/usr/bin/gpg --yes --homedir /home/.gnupg --output /tmp/update_dec.tar --decrypt /tmp/$filename";
	    	exec("$decripta 2>&1", $out1, $gpgerror);

		if ( $gpgerror == 0 ) {

                        echo _("<center>");
                        echo _("The signature of the file <b>$filename</b> is right.\nPlease wait while installation in process.\n");
                        echo _("\n");

			} else {

                            echo _("<center>");
                            echo _("The signature of the file <b>$filename</b> is bad.\nThe installation was interrupted.\n");
                            echo _("\n");
                		    $rimuovi_file_temporanei="/bin/rm -rf /tmp/*";
                		    exec("$rimuovi_file_temporanei 2>&1");
				            break;
		}
	
		sleep(1);
	
	    	$scompatta="/bin/tar xpf /tmp/update_dec.tar -C /tmp";
	    	exec("$scompatta 2>&1", $out2, $tarerror);

			foreach($out2 as $outputline){
   				echo("$outputline<br>");
				}
		sleep(1);

	    	$esegue_setup="sudo /tmp/install";
	    	exec("$esegue_setup 2>&1", $out3, $installerror);
				
			foreach($out3 as $outputline){
   				echo("$outputline<br>");
				}

		sleep(1);

	    	$rimuovi_file_4="/bin/rm -f /tmp/$filename";
	    	exec("$rimuovi_file_4 2>&1", $out4, $removeerror);

			foreach($out4 as $outputline){
   				echo("$outputline<br>");
				}

		sleep(1);

    		print "</center>";

		if ($installerror != 255) {

                	exec ('sudo /sbin/shutdown -r now');

		}

             }else{

               echo "<font color='#FF0000'>";
               echo _("There is a errors in your file upload.");
               echo "</font><br>";
               echo "<a href='javascript:history.back()'>";
               echo _("Cick here");
               echo "</a>";
               echo "&nbsp;";
               echo _("for back to file selection.");
             }

           }else{

             echo "<font color='#FF0000'>";
             echo _("The filesize is not right.");
             echo "</font>\n";
             echo "<a href='javascript:history.back()'>";
             echo _("Cick here");
             echo "</a>";
             echo "&nbsp;";
             echo _("for back to file selection.");
           }

         }else{

           echo "<font color='#FF0000'>";
           echo _("The file extension is not right.");
           echo "</font>\n";
           echo "<a href='javascript:history.back()'>";
           echo _("Cick here");
           echo "</a>";
           echo "&nbsp;";
           echo _("for back to file selection.");
         }

       }else{

         echo "<font color='#FF0000'>";
         echo _("There is a errors during the Software Updater.");
         echo "</font>\n";
       	 echo "<a href='javascript:history.back()'>";
       	 echo _("Cick here");
       	 echo "</a>";
       	 echo "&nbsp;";
       	 echo _("for back to file selection.");
       }
     }else{

       echo "<font color='#FF0000'>";
       echo _("No filename selected.");
       echo "</font>\n";
       echo "<a href='javascript:history.back()'>";
       echo _("Cick here");
       echo "</a>";
       echo "&nbsp;";
       echo _("for back to file selection.");
     }
   }
  }
}
?>
<HTML>
<HEAD><TITLE><?php echo _("Voiper PBX Web updater")?></TITLE>
<link href="home.css" rel="stylesheet" type="text/css">
</HEAD>

<BODY text="#000000" vLink="#000000" aLink="#000000" link="#000000" bgColor="#eeeeee">

<pre>
<center>

<? echo uploader(); ?>

</center>
</pre>
</BODY></HTML>

